package com.lyl.momanapp.frament;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.ContentUris;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lyl.momanapp.R;
import com.lyl.momanapp.adapter.OneOneAdapter;
import com.lyl.momanapp.adapter.OneOneimgAdapter;
import com.lyl.momanapp.adapter.TwoTwoAdapter;
import com.lyl.momanapp.adapter.TwoTwoBJAdapter;
import com.lyl.momanapp.adapter.TwoTwoimgAdapter;
import com.lyl.momanapp.bean.CJBean;
import com.lyl.momanapp.bean.Material;
import com.lyl.momanapp.bean.Tag;
import com.lyl.momanapp.bean.URL;
import com.lyl.momanapp.util.utlis.MyUtils;
import com.lyl.momanapp.util.view.MyFragment;

import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 名字 梁亚龙
 * 时间 2017/9/11.
 * QQ邮箱 503934353@qq.com
 */

public class TwoOne extends MyFragment {
    private RecyclerView mRl;
    private RecyclerView mRlimg;
    private OneOneAdapter mOneOneAdapter;
    private OneOneimgAdapter mOneOneimgAdapter;
    private int positionname;
    private ImageView mImg;
    private Material material;
    public static final int SELECT_PHOTO = 2;
    private TwoTwoAdapter twoTwoAdapter;
    private TwoTwoBJAdapter twoTwoimgAdapter;

    @Override
    public void onClick(View view) {

    }

    @Override
    public int inflaterRootView() {
        return R.layout.frament_one_one;
    }

    @Override
    public void initUI() {
        mRl = ((RecyclerView) findView(R.id.lian_dong_rl_name));
        mRlimg = ((RecyclerView) findView(R.id.lian_dong_rl_img));
        mImg = ((ImageView) findView(R.id.frament_one_one_img));
        mImg.setImageDrawable(getResources().getDrawable(R.drawable.tou1));
        mImg.setDrawingCacheEnabled(false);
    }

    @Override
    public void initData() {
    }

    @Override
    public void addListeners() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        mRl.setLayoutManager(linearLayoutManager);
        mRl.setHasFixedSize(true);
        twoTwoAdapter = new TwoTwoAdapter(new String[]{"背景"});
        mRl.setAdapter(twoTwoAdapter);
        twoTwoAdapter.setOnItemClickLitener(new TwoTwoAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(TextView view, int position) {
                mRlimg.setAdapter(twoTwoimgAdapter);
                twoTwoimgAdapter.showDataString(new String[]{"1", "2"});
            }
        });
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        mRlimg.setLayoutManager(linearLayoutManager2);
        mRlimg.setHasFixedSize(true);
        twoTwoimgAdapter = new TwoTwoBJAdapter(new String[]{}, getActivity());
        twoTwoimgAdapter.showDataString(new String[]{"1", "2"});
        mRlimg.setAdapter(twoTwoimgAdapter);

        twoTwoimgAdapter.setOnItemClickLitener(new TwoTwoBJAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(ImageView view, int position) {
                if (position == 0) {
                    CJBean.beijing = 1;
                    mImg.setImageDrawable(getResources().getDrawable(R.drawable.tou1));
                    mImg.setDrawingCacheEnabled(false);
                } else {
                    CJBean.beijing = 2;
                    mImg.setImageDrawable(getResources().getDrawable(R.drawable.tou2));
                    mImg.setDrawingCacheEnabled(false);
                }
            }
        });
    }

    public void select_photo() {
        if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        } else {
            openAlbum();
        }
    }

    private void openAlbum() {
        Intent intent = new Intent("android.intent.action.GET_CONTENT");
        intent.setType("image/*");
        startActivityForResult(intent, SELECT_PHOTO);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case SELECT_PHOTO:
                if (resultCode == RESULT_OK) {
                    //判断手机系统版本号
                    if (Build.VERSION.SDK_INT > 19) {
                        //4.4及以上系统使用这个方法处理图片
                        handleImgeOnKitKat(data);
                    } else {
                        handleImageBeforeKitKat(data);
                    }
                }
                break;
            default:
                break;
        }
    }

    /**
     * 4.4以下系统处理图片的方法
     */
    private void handleImageBeforeKitKat(Intent data) {
        Uri uri = data.getData();
        String imagePath = getImagePath(uri, null);
        displayImage(imagePath);
    }


    /**
     * 4.4及以上系统处理图片的方法
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void handleImgeOnKitKat(Intent data) {
        String imagePath = null;
        Uri uri = data.getData();
        if (DocumentsContract.isDocumentUri(getContext(), uri)) {
            //如果是document类型的uri，则通过document id处理
            String docId = DocumentsContract.getDocumentId(uri);
            if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
                //解析出数字格式的id
                String id = docId.split(":")[1];
                String selection = MediaStore.Images.Media._ID + "=" + id;
                imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
            } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
                Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
                imagePath = getImagePath(contentUri, null);
            } else if ("content".equalsIgnoreCase(uri.getScheme())) {
                //如果是content类型的uri，则使用普通方式处理
                imagePath = getImagePath(uri, null);
            } else if ("file".equalsIgnoreCase(uri.getScheme())) {
                //如果是file类型的uri，直接获取图片路径即可
                imagePath = uri.getPath();
            }
            //根据图片路径显示图片
            displayImage(imagePath);
        }
    }

    /**
     * 通过uri和selection来获取真实的图片路径
     */
    private String getImagePath(Uri uri, String selection) {
        String path = null;
        Cursor cursor = getContext().getContentResolver().query(uri, null, selection, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            }
            cursor.close();
        }
        return path;
    }

    /**
     * 根据图片路径显示图片的方法
     */
    private void displayImage(String imagePath) {
        if (imagePath != null) {
            x.image().bind(mImg, imagePath);
            Glide.with(getActivity()).load(imagePath).into(mImg);
            URL.ONEONEimg = imagePath;

        } else {

        }
    }

}
